﻿using System;
using System.Web.UI.WebControls;

namespace ECommerce.Administration.Controls
{
	public partial class CustomerList : AdminBaseUsercontrol
	{
		private string[] _roles;

		protected void Page_PreRender(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				ButtonSearch.Text = GetLocalResourceString("Admin.SearchButton.Text");

				BindRoles();
			}

			BindUsers();
		}

		protected void ButtonSearch_Click(object sender, EventArgs e)
		{
		}

		protected void GridUsers_RowUpdating(object sender, GridViewUpdateEventArgs e)
		{
			var hfUserName = GridUsers.Rows[e.RowIndex].FindControl("hfUserName") as HiddenField;
			var ListRole = GridUsers.Rows[e.RowIndex].FindControl("ListRole") as DropDownList;

			foreach(ListItem item in ListRole.Items)
			{
				if(ListRole.SelectedItem.Value == item.Value)
				{
					System.Web.Security.Roles.AddUserToRole(hfUserName.Value, item.Value);
				}
				else
				{
					System.Web.Security.Roles.RemoveUserFromRole(hfUserName.Value, item.Value);
				}
			}
		}

		protected void GridUsers_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if(e.Row.RowType == DataControlRowType.Header)
			{
				e.Row.Cells[1].Text = GetLocalResourceString("Admin.UserList.NameHeader");
				e.Row.Cells[2].Text = GetLocalResourceString("Admin.UserList.EmailHeader");
				e.Row.Cells[3].Text = GetLocalResourceString("Admin.UserList.CreationDateHeader");
				e.Row.Cells[4].Text = GetLocalResourceString("Admin.UserList.LastLoginDateHeader");
				e.Row.Cells[5].Text = GetLocalResourceString("Admin.UserList.RoleHeader");
			}
			else if(e.Row.DataItem != null)
			{
				var ListRole = e.Row.FindControl("ListRole") as DropDownList;
				var ButtonUpdateRole = e.Row.FindControl("ButtonUpdateRole") as Button;

				ButtonUpdateRole.Text = GetLocalResourceString("Admin.UpdateRoleButton.Text");

				BindListRole(ListRole);

				var hfUserName = e.Row.FindControl("hfUserName") as HiddenField;
				var userName = hfUserName.Value;
				var roles = System.Web.Security.Roles.GetRolesForUser(userName);
				ListRole.SelectedIndex = ListRole.Items.IndexOf(ListRole.Items.FindByValue(roles[0]));
			}
		}

		private void BindListRole(DropDownList theDropDownList)
		{
			if(_roles == null)
			{
				_roles = System.Web.Security.Roles.GetAllRoles();
			}

			ListItemCollection rolesList = new ListItemCollection();

			foreach(var r in _roles)
			{
				rolesList.Add(new ListItem(r, r));
			}

			theDropDownList.DataSource = rolesList;
			theDropDownList.DataBind();
		}

		private void BindRoles()
		{
			if(_roles == null)
			{
				_roles = System.Web.Security.Roles.GetAllRoles();
			}

			ListItemCollection rolesList = new ListItemCollection();

			rolesList.Add(new ListItem(GetLocalResourceString("Admin.UserList.SelectRolesTitle"), "all"));

			foreach(var r in _roles)
			{
				//data.Add(new Role(r, ""));
				rolesList.Add(new ListItem(r, r));
			}

			DropDownListRoles.DataSource = rolesList;
			DropDownListRoles.DataBind();
		}

		private void BindUsers()
		{
			var selectedRoleName = DropDownListRoles.SelectedValue.Trim();

			System.Web.Security.MembershipUserCollection membershipUsers = new System.Web.Security.MembershipUserCollection();

			if(!System.Web.Security.Roles.RoleExists(selectedRoleName))
			{
				membershipUsers = System.Web.Security.Membership.GetAllUsers();
			}
			else
			{
				var users = System.Web.Security.Roles.GetUsersInRole(selectedRoleName);

				foreach(var user in users)
				{
					membershipUsers.Add(System.Web.Security.Membership.GetUser(user));
				}
			}

			if(!string.IsNullOrEmpty(TextUserName.Text))
			{
				GridUsers.DataSource = System.Web.Security.Membership.FindUsersByName(TextUserName.Text + "%");
				GridUsers.DataBind();
			}
			else
			{
				GridUsers.DataSource = membershipUsers;
				GridUsers.DataBind();
			}
		}

		public void DeleteSelectedUsers()
		{
			foreach(System.Web.UI.WebControls.GridViewRow rowItem in GridUsers.Rows)
			{
				var chBox = rowItem.FindControl("cbUser") as CheckBox;
				var hfUserName = rowItem.FindControl("hfUserName") as HiddenField;

				bool isChecked = chBox.Checked;
				string userName = hfUserName.Value;

				if(isChecked)
				{
					System.Web.Security.Membership.DeleteUser(userName);
				}
			}

			BindUsers();
		}
	}
}